package local_database;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlReader {
	DocumentBuilderFactory docFactory;
	DocumentBuilder docBuilder = null;
	Document doc = null;
	String file;

	public XmlReader(String file) {
		this.file = file;
		docFactory = DocumentBuilderFactory.newInstance();
		try {
			docBuilder = docFactory.newDocumentBuilder();
			doc = docBuilder.parse(file);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	}

	public String getRootnode() {
		String rootName;
		Node root = null;
		root = doc.getFirstChild();
		rootName = root.getNodeName();
		return rootName;
	}

	public String getNodeTextContent(String elementName) {
		String childNodeName;
		Node childNode = null;
		try {
			childNode = doc.getElementsByTagName(elementName).item(0);
			childNodeName = childNode.getTextContent();
		} catch (Exception e) {
			childNodeName = "N/A";
		}
		return childNodeName;
	}
	
	public String getNodeTextContent(String parent, String parentAttr, String attrValue, String childName){
		String content = "";
		Node childNode = null;
		try {
			NodeList childList = doc.getElementsByTagName(parent);
			for (int i = 0; i < childList.getLength(); i++) {
				childNode = childList.item(i);
				Node attrNode = childNode.getAttributes().getNamedItem(parentAttr);
				if((attrNode.getTextContent()).equals(attrValue)){
					NodeList childList2 = childNode.getChildNodes();
					content = searchNodeChild(childName, content, childList2);		
				}
			}
			
		} catch (Exception e) { 
			content = "N/A";
		}
		return content;
	}

	private String searchNodeChild(String childName, String content,
			NodeList childList2) {
		for(int x=0;x<childList2.getLength();x++){
			Node child2 = childList2.item(x);
			if((child2.getNodeName()).equals(childName)){
				content = child2.getTextContent();
			}
		}
		return content;
	}

	public ArrayList getNodesTextContent(String elementName) {
		ArrayList list = new ArrayList();
		NodeList nodeList = doc.getElementsByTagName(elementName);
		for (int i = 0; i < nodeList.getLength(); i++) {
			list.add(nodeList.item(i).getTextContent());
		}
		return list;
	}
	

	public String getNodeAttrContent(String elementName, String AttrName) {
		String childNodeAttr;
		Node childNode = null;
		NamedNodeMap attr;
		try {
			childNode = doc.getElementsByTagName(elementName).item(0);
			attr = childNode.getAttributes();
			Node nodeAttr = attr.getNamedItem(AttrName);
			childNodeAttr = nodeAttr.getTextContent();
		} catch (Exception e) {
			childNodeAttr = "N/A";
		}
		return childNodeAttr;
	}
	
	public ArrayList getNodesAttrContent(String elementName, String AttrName) {
		ArrayList list = new ArrayList();
		NodeList childNodeList = null;
		String childNodeAttr;
		NamedNodeMap attr;
		try {
			childNodeList = doc.getElementsByTagName(elementName);
			for(int i=0;i<childNodeList.getLength();i++){
				Node childNode = childNodeList.item(i);
				attr = childNode.getAttributes();
				Node nodeAttr = attr.getNamedItem(AttrName);
				childNodeAttr = nodeAttr.getTextContent();
				list.add(childNodeAttr);
			}
			
		} catch (Exception e) {
			
		}
		return list;
	}
	
	

}
